using System;
using System.Collections.Generic;
using System.Linq;
using Survival.Animals;
using Survival.Services;
using Survival.Plants;
using Survival.Player;
using Survival.Towns;
using Survival.Components;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Survival
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Survival : Microsoft.Xna.Framework.Game
    {
        InputHandler inputHandler;
        AnimalManager animalManager;
        PlantManager plantManager;
        UserInterface userInterface;
        Town town;
        PlayerCharacter playerCharacter;
        WorldQuery worldQuery;
        GraphicsDeviceManager graphics;
        DebugText debugText;
        SpriteBatch spriteBatch;
        
        public Texture2D noSprite;

        public Survival()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            AddComponents();
            CreateServices();
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            this.IsFixedTimeStep = false;
            //this.graphics.SynchronizeWithVerticalRetrace = false;
            graphics.ApplyChanges();
            base.Initialize();
        }

        private void AddComponents()
        {
            inputHandler = new InputHandler(this);
            Components.Add(inputHandler);
            inputHandler.UpdateOrder = 1;

            plantManager = new PlantManager(this);
            Components.Add(plantManager);
            plantManager.UpdateOrder = 2;

            town = new Town(this);
            Components.Add(town);
            town.UpdateOrder = 3;

            playerCharacter = new PlayerCharacter(this);
            Components.Add(playerCharacter);
            playerCharacter.UpdateOrder = 4;

            animalManager = new AnimalManager(this);
            Components.Add(animalManager);
            animalManager.UpdateOrder = 4;

            userInterface = new UserInterface(this);
            Components.Add(userInterface);
            userInterface.UpdateOrder = 5;

            debugText = new DebugText(this);
            Components.Add(debugText);
            debugText.UpdateOrder = 3;

            worldQuery = new WorldQuery(animalManager, plantManager);
        }

        private void CreateServices()
        {
            Services.AddService(typeof(IInputService), inputHandler);
            Services.AddService(typeof(IDebugTextService), debugText);
            Services.AddService(typeof(IWorldQuery), worldQuery);
            Services.AddService(typeof(IUserInterface), userInterface);
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {

            spriteBatch = new SpriteBatch(GraphicsDevice);
            noSprite = Content.Load<Texture2D>("No");
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {

        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.DarkGreen);
            
            base.Draw(gameTime);
        }
    }
}
